package com.example.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.model.entity.UserInfo;
import com.example.model.dto.LoginRequest;
import com.example.model.dto.RegisterRequest;
import com.example.model.dto.AddTeacherRequest;
import com.example.mapper.UserInfoMapper;
import com.example.service.UserService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class UserServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>  implements UserService {
    
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo login(LoginRequest loginRequest) {
        UserInfo userInfo = userInfoMapper.findByAccountAndRoleAndPassword(
            loginRequest.getAccount(), 
            loginRequest.getRole(),
            loginRequest.getPassword()
        );
        
        if (userInfo == null) {
            throw new RuntimeException("账号、密码或角色错误");
        }
        
        userInfo.setPassword(null);
        return userInfo;
    }
    
    @Override
    public UserInfo register(RegisterRequest registerRequest) {
        // 检查账号是否已存在
        UserInfo existUser = userInfoMapper.findByAccount(registerRequest.getAccount());
        if (existUser != null) {
            throw new RuntimeException("账号已存在");
        }
        
        // 创建新用户
        UserInfo userInfo = new UserInfo();
        String datePrefix = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String randomSuffix = String.format("%04d", RandomUtil.randomInt(0, 10000));
        String code = datePrefix + randomSuffix;
        userInfo.setCode(code);
        userInfo.setAccount(registerRequest.getAccount());
        userInfo.setPassword(registerRequest.getPassword());
        userInfo.setSex(registerRequest.getSex());
        userInfo.setRole(2); // 注册用户默认为学生角色
        
        save(userInfo);

        userInfo.setUserName(userInfo.getId() + "学生【默认名称】");
        updateById(userInfo);
        
        userInfo.setPassword(null);
        return userInfo;
    }
    
    @Override
    public UserInfo addTeacher(AddTeacherRequest addTeacherRequest) {
        // 检查账号是否已存在
        UserInfo existUser = userInfoMapper.findByAccount(addTeacherRequest.getAccount());
        if (existUser != null) {
            throw new RuntimeException("账号已存在");
        }
        
        // 创建教师用户
        UserInfo userInfo = new UserInfo();
        String datePrefix = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String randomSuffix = String.format("%04d", RandomUtil.randomInt(0, 10000));
        String code = datePrefix + randomSuffix;
        userInfo.setCode(code);
        userInfo.setUserName(addTeacherRequest.getUserName());
        userInfo.setAccount(addTeacherRequest.getAccount());
        userInfo.setPassword(addTeacherRequest.getPassword());
        userInfo.setSex(addTeacherRequest.getSex());
        userInfo.setRole(1); // 教师角色
        
        save(userInfo);
        
        userInfo.setPassword(null);
        return userInfo;
    }

    @Override
    public List<UserInfo> teacherList( ) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getRole,1);
        return list(queryWrapper);
    }

    @Override
    public List<UserInfo> studentList() {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getRole,2);
        return list(queryWrapper);
    }

    @Override
    public Boolean deleteTec(UserInfo userInfo) {
        Integer id = userInfo.getId();
        return removeById(id);
    }
} 